//
//  ChangeLoginPwdView.swift
//  cxd4iphone
//
//  Created by hexy on 12/26/15.
//  Copyright © 2015 hexy. All rights reserved.
//

import UIKit
import ReactiveCocoa

class ChangeLoginPwdView: UIView {


    override init(frame: CGRect) {
        super.init(frame: frame)
        
        makeUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func makeUI() {
        
        //                let wrap = UIView(bgColor: xx_randomColor())
        let wrap = UIView()
        
        addSubview(wrap)
        wrap.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(UIEdgeInsetsMake(15, 15, -15, -15))
        }
        
        
        wrap.addSubview(oldPwdTextField)
        oldPwdTextField.snp_makeConstraints { (make) -> Void in
            
            make.top.left.right.equalTo(wrap)
            make.height.equalTo(xx_height(40))
            
        }
        
        wrap.addSubview(newPwdTextField)
        newPwdTextField.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(oldPwdTextField.snp_bottom).offset(xx_height(20))
            make.left.right.equalTo(wrap)
            make.height.equalTo(xx_height(40))
            
        }
        
        wrap.addSubview(surePwdTextField)
        surePwdTextField.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(newPwdTextField.snp_bottom).offset(xx_height(20))
            make.left.right.equalTo(wrap)
            make.height.equalTo(xx_height(40))
            
        }

        
        wrap.addSubview(submitBtn)
        submitBtn.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(surePwdTextField.snp_bottom).offset(xx_height(30))
            make.left.right.equalTo(wrap)
            make.height.equalTo(xx_height(40))
            
        }
        
        
    }
    
    
    lazy var oldPwdTextField = UITextField(placeholder: "原始密码")
    
    lazy var newPwdTextField = UITextField(placeholder: "新密码")
    
    lazy var surePwdTextField = UITextField(placeholder: "确认新密码")
    lazy var submitBtn: UIButton = {
        
        let btn = UIButton(title: "提交", titleColor: UIColor.whiteColor(), titleSize: 18, bgColor: Define.blueColor)
        
        btn.setBackgroundImage(UIImage.createImgaeWithColor(Define.grayColor), forState: .Disabled)

        RACSignal.combineLatest([self.oldPwdTextField.rac_textSignal(), self.newPwdTextField.rac_textSignal(), self.surePwdTextField.rac_textSignal()]).map { (tuple) -> AnyObject! in
            
                return self.isValid(tuple as! RACTuple)

            }.subscribeNext { (enable) -> Void in

                btn.enabled =  (enable as! Bool)
        }
        
        return btn
    }()
    
    func isValid(tuple: RACTuple) -> Bool {
        
        let oldPwd = tuple.first as! NSString
        let newPwd = tuple.second as! NSString
        let surePwd = tuple.third as! NSString
        
        if oldPwd.length > 0 && newPwd.length > 0 && surePwd.length > 0 {
            
            if newPwd != surePwd {
                
                return false
            }
            return true
        }
        return false
    }
}
